package br.sales.view.listener;

import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

import br.sales.controller.ConfigurationControllerImpl;
import br.sales.entity.Configuration;
import br.sales.entity.ConfigurationValues;



@WebListener(value="appSessionListener")
public class AppSessionListener implements HttpSessionListener{

	@Override
	public void sessionCreated(HttpSessionEvent se) {
		try{
			
			Configuration timeout = ConfigurationControllerImpl.getInstance().retrieveByName(ConfigurationValues.sessionTimeout);
			se.getSession().setMaxInactiveInterval(Integer.parseInt(timeout.getValue())*60);
			System.out.println("NEW SESSION - "+se.getSession().getId());
			
		}catch (Exception e) {
			se.getSession().setMaxInactiveInterval(40*60);
			e.printStackTrace();
		}
	}

	@Override
	public void sessionDestroyed(HttpSessionEvent se) {
		System.out.println("END SESSION - "+se.getSession().getId());
	}

}
